package com.nd.android.musicboxlibrary.ui.common;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.StarAppConst;
import com.common.allCommon.CommonUtils;
import com.common.android.utils.FileUtils;
import com.common.utils.StarAppUtils;
import com.common.widget.CommonCustomDlg;
import com.common.widget.CommonCustomDlg.OnCommonDlgClickListener;
import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.ui.vip.VipIntroduceActivity;
import com.nd.android.musicboxlibrary.ui.vip.VipUtil;
import com.nd.android.musicboxlibrary.util.ToastUtil;

/**
 * 下载弹出选择音乐的弹出框
 * Created 2014-11-20 下午19:57:06
 * @author jiangwb
 * 
 * 关于CommonUtils.isJayVersion()的使用
 *  99U 聊天工具
 * 黄江 to 陈艺伟 
 *  2014-12-11 14:25:10
 * 周航是不希望杰伦里面有这些，让用户感觉到我们在利用杰伦
 * 特别是app里面没什么东西还做vip
 */
public class DownloadSelectDialogUtil {

	public interface OnClickListener{
		public void onRightBtnClicked(boolean isHQ);
	}

	/**
	 * 显示下载弹出框 
	 * Created 2014-11-17 下午15:57:06
	 * @param cqSize 普通音质的大小,单位是字节（K）
	 * @param hqSize HQ音质的大小,单位是字节（K）
	 * @author jiangwb
	 */
	public static void showDownloadDlg(final Context context,
			final boolean isVipTag ,
			final MusicInfo music,
			final OnClickListener listener){
		//VIP且高清文件未下载过，可以弹框给下载
		if( VipUtil.fileExist(music.getHQMusicPath()) ){
			ToastUtil.showLong( context, R.string.mbx_jaymusic_download_done, false );
			return;
		}

		final long cqSize = music.getSize();
		final long hqSize = music.getHQSize();
		final long hqFid = music.getHQId();

		CommonCustomDlg ctd = new CommonCustomDlg(context,
				context.getString(R.string.mbx_dialog_download_title),
				context.getString(R.string.mbx_dialog_download_cancel),
				context.getString(R.string.mbx_dialog_download_ok),
				R.style.common_dialog_style);
		ctd.setLayoutResID(R.layout.common_costom_dlg_pink);
		View mCustomView = ((LayoutInflater)context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.mbx_player_download_dlg, null);
		final TextView ceSizeTV = (TextView)mCustomView
				.findViewById(R.id.mbx_dialog_download_common_quality_tv);
		final TextView hqSizeTV = (TextView)mCustomView
				.findViewById(R.id.mbx_dialog_download_hq_quality_tv);
		final ImageView commonQualityBtn = (ImageView)mCustomView
				.findViewById(R.id.mbx_dialog_download_common_quality_btn);
		final ImageView hqQualityBtn = (ImageView)mCustomView
				.findViewById(R.id.mbx_dialog_download_hq_quality_btn);
		final View cqLayout = mCustomView
				.findViewById(R.id.mbx_dialog_download_common_quality_layout);
		final View hqLayout = mCustomView
				.findViewById(R.id.mbx_dialog_download_hq_quality_layout);
		ImageView vipBtn = (ImageView)mCustomView
				.findViewById(R.id.mbx_dialog_download_vip_btn);

		ceSizeTV.setText(context.getString(R.string.mbx_dialog_download_common_quality, 
				FileUtils.getFileLengthUnitMB(cqSize, true)));
		hqSizeTV.setText(context.getString(R.string.mbx_dialog_download_hq_quality,
				FileUtils.getFileLengthUnitMB(hqSize, true)));
        //字体
        StarAppUtils.setViewTypeFace(ceSizeTV, false);
        StarAppUtils.setViewTypeFace(hqSizeTV, false);

		//		if(isVipTag && hqSize > 0){//VIP用户默认就选择是HQ音质,如果没有HQ的文件，那么不可选
		if(isVipTag){
			hqQualityBtn.setImageResource(R.drawable.setting_select_font_selected);
			commonQualityBtn.setImageResource(R.drawable.setting_select_font_normal);
			ceSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_gray));
			hqSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_pink));
			commonQualityBtn.setTag(false);
			hqQualityBtn.setTag(true);
		}else{
			commonQualityBtn.setImageResource(R.drawable.setting_select_font_selected);
			hqQualityBtn.setImageResource(R.drawable.setting_select_font_normal);
			ceSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_pink));
			hqSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_gray));
			commonQualityBtn.setTag(true);
			hqQualityBtn.setTag(false);
		}

		if(hqFid > 0){
			hqLayout.setVisibility(View.VISIBLE);
		}else{
			hqLayout.setVisibility(View.GONE);
		}

		cqLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				commonQualityBtn.setImageResource(R.drawable.setting_select_font_selected);
				hqQualityBtn.setImageResource(R.drawable.setting_select_font_normal);
				ceSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_pink));
				hqSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_gray));
				commonQualityBtn.setTag(true);
				hqQualityBtn.setTag(false);
			}
		});
		hqLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if((CommonUtils.isJayVersion()  || isVipTag )&& hqSize > 0){//非VIP用户是不能选择VIP功能,如果没有HQ的文件，那么VIP用户也不可选
					hqQualityBtn.setImageResource(R.drawable.setting_select_font_selected);
					commonQualityBtn.setImageResource(R.drawable.setting_select_font_normal);
					ceSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_gray));
					hqSizeTV.setTextColor(context.getResources().getColor(R.color.mbx_dlg_font_pink));
					commonQualityBtn.setTag(false);
					hqQualityBtn.setTag(true);
				}else{
					if(!CommonUtils.isJayVersion()  && !isVipTag ){
						//进入到VIP介绍的页面
						context.startActivity(new Intent(context, VipIntroduceActivity.class));		
					}
				}
			}
		});

		if(CommonUtils.isJayVersion() ){
			// 99U 聊天工具
			//黄江 to 陈艺伟 
			// 2014-12-11 14:25:10
			//周航是不希望杰伦里面有这些，让用户感觉到我们在利用杰伦
			//特别是app里面没什么东西还做vip
			vipBtn.setVisibility(View.GONE);
		}

		vipBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//进入到VIP介绍的页面
				context.startActivity(new Intent(context, VipIntroduceActivity.class));				
			}
		});

		ctd.setCustomView(mCustomView);
		ctd.setOnListener(new OnCommonDlgClickListener() {

			@Override
			public void onRightBtnClicked() {
				if(listener != null){
					Object obj = hqQualityBtn.getTag();
					boolean isDownloadHQ = false;
					if(obj != null && obj instanceof Boolean && (Boolean)obj){
						isDownloadHQ = true;
					}
					if(!CommonUtils.isJayVersion()  && isDownloadHQ && !isVipTag ){
						//进入到VIP介绍的页面,by jiangwb 如果是非VIP用户，下载，就进入VIP结束吧！
						context.startActivity(new Intent(context, VipIntroduceActivity.class));	
						return;
					}
					listener.onRightBtnClicked(isDownloadHQ);
				}
			}

			@Override
			public void onLeftBtnClicked() {
				//取消不做任何操作
			}
		});

		ctd.setCanceledOnTouchOutside(true);
		ctd.show();
	}
}
